package com.hellogood.yaohuo.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hellogood.yaohuo.server.db.DBConnection;
import com.hellogood.yaohuo.server.db.DBHelper;
import com.hellogood.yaohuo.shared.entity.Detail;

public class DetailDAO extends DBHelper {

	@Override
	public List<?> initializeObject(ResultSet rs) throws SQLException {
		List<Detail> detailList = new ArrayList<Detail>();
		boolean hasClsCode = false;
		boolean hasClsName = false;
		boolean hasUnit = false;
		boolean hasDeptName = false;
		boolean hasSglCount = false;
		boolean hasPsPrice = false;
		boolean hasPsCost = false;
		boolean hasJPrice = false;
		boolean hasPrice = false;
		boolean hasCost = false;
		boolean hasTotal = false;
		boolean hasColorName = false;
		int columns = rs.getMetaData().getColumnCount();
		for (int i = 1; i < columns + 1; i++) {
			String columnName = rs.getMetaData().getColumnName(i);
			if (columnName.equals("ClsCode")) {
				hasClsCode = true;
			} else if (columnName.equals("ClsName")) {
				hasClsName = true;
			} else if (columnName.equals("UNIT")) {
				hasUnit = true;
			} else if (columnName.equals("DepName")) {
				hasDeptName = true;
			} else if (columnName.equals("sglCount")) {
				hasSglCount = true;
			} else if (columnName.equals("PsPrice")) {
				hasPsPrice = true;
			} else if (columnName.equals("psCost")) {
				hasPsCost = true;
			} else if (columnName.equals("jPrice") || columnName.equals("JPrice")) {
				hasJPrice = true;
			} else if (columnName.equals("Price")) {
				hasPrice = true;
			} else if (columnName.equals("Cost")) {
				hasCost = true;
			} else if (columnName.equals("Total")) {
				hasTotal = true;
			} else if (columnName.equals("colorName")) {
				hasColorName = true;
			}
		}
		if (rs != null) {
			while (rs.next()) {
				Detail d = new Detail();
				d.setBillNo(rs.getString("billNo") == null ? "" : rs.getString("billNo").trim());
				d.setPluCode(rs.getString("pluCode") == null ? "" : rs.getString("pluCode").trim());
				d.setPluName(rs.getString("pluName") == null ? "" : rs.getString("pluName").trim());
				d.setBarCode(rs.getString("barCode") == null ? "" : rs.getString("barCode").trim());
				d.setBra(rs.getString("barCode") == null ? "" : rs.getString("barCode").trim());
				d.setColor(rs.getString("color"));
				if (hasColorName) {
					d.setColor(rs.getString("color") + "-" + rs.getString("colorName"));
				}
				if (rs.getString("color").equals("0")) {
					d.setColor(rs.getString("color") + "-均色");
				}
				if (hasUnit) {
					d.setUnit(rs.getString("unit"));
				}
				if (hasClsCode) {
					d.setClsCode(rs.getString("clsCode") == null ? "" : rs.getString("clsCode").trim());
				}
				if (hasClsName) {
					d.setClsName(rs.getString("clsName") == null ? "" : rs.getString("clsName").trim());
				}
				if (hasSglCount) {
					d.setSglCount(rs.getInt("sglCount"));
				}
				if (hasJPrice) {
					d.setjPrice(rs.getBigDecimal("jPrice") == null ? rs.getBigDecimal("JPrice") : rs.getBigDecimal("jPrice"));
					d.setNetJPrice(rs.getBigDecimal("netJPrice"));
				}
				if (hasPrice) {
					d.setPrice(rs.getBigDecimal("price"));
				}
				d.setCounts(rs.getInt("counts"));
				d.setSize_s(rs.getInt("size_s"));
				d.setSize_m(rs.getInt("size_m"));
				d.setSize_l(rs.getInt("size_l"));
				d.setSize_xl(rs.getInt("size_xl"));
				d.setSize_xxl(rs.getInt("size_xxl"));
				d.setSize_xxxl(rs.getInt("size_xxxl"));
				d.setSize_avg(rs.getInt("size_avg"));
				if (hasCost) {
					d.setCost(rs.getBigDecimal("cost"));
					d.setNetCost(rs.getBigDecimal("netCost"));
				}
				if (hasPsPrice) {
					d.setPsPrice(rs.getBigDecimal("PsPrice"));
				}
				if (hasPsCost) {
					d.setPsCost(rs.getBigDecimal("psCost"));
				}
				if (hasTotal) {
					d.setTotal(rs.getBigDecimal("total"));
				}
				d.setTimeMark(rs.getString("timeMark"));
				d.setRzDate(rs.getString("rzDate"));
				d.setRemark(rs.getString("remark"));
				d.setOrgCode(rs.getString("orgCode") == null ? "" : rs.getString("orgCode").trim());
				d.setDepCode(rs.getString("depCode") == null ? "" : rs.getString("depCode").trim());
				if (hasDeptName) {
					d.setDepName(rs.getString("depName") == null ? "" : rs.getString("depName").trim());
				}
				detailList.add(d);
			}
		}
		return detailList;
	}

	@SuppressWarnings("unchecked")
	public List<Detail> getDetailByBillNo(String billNo, String tableDetailName, String billNoField) {
		List<Detail> list = new ArrayList<Detail>();
		String hxDbName = DBConnection.hxDbName;
		String sql = "SELECT (SELECT depName FROM " + hxDbName
				+ "..dept WHERE DepCode = d.depcode) AS depName,(SELECT colorName FROM color WHERE ColorCode = d.color) AS colorName,(SELECT clsCode FROM " + hxDbName
				+ "..GClass WHERE ClsCode = (SELECT ClsCode FROM " + hxDbName + "..Goods WHERE d.pluCode = pluCode)) AS ClsCode,(SELECT clsName FROM " + hxDbName
				+ "..GClass WHERE ClsCode = (SELECT ClsCode FROM " + hxDbName + "..Goods WHERE d.pluCode = pluCode)) AS ClsName,(SELECT UNIT FROM " + hxDbName
				+ "..Goods WHERE d.pluCode = pluCode) AS UNIT,d.*,d." + billNoField + " AS billNo FROM " + tableDetailName + " d WHERE d." + billNoField + " = '" + billNo + "' ORDER BY TimeMark";
		list = (List<Detail>) super.query(sql);
		return list;
	}
}
